package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import service.MessageHandler;

public class MessageSenderThread extends Thread{
        private MessageHandler messageHandler;
        
        public MessageSenderThread (MessageHandler handler) {
                messageHandler = handler;
                start();
        }
        
        public void run() {
                String message = null;
                BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
                
                while (!isInterrupted()) {
                        try {
                                message = input.readLine();
                                
                                messageHandler.sendMessage(message);
                        } catch (IOException e) {
                                System.out.println("Connection with server is lost");
                        }
                        if (message.equals("/exit")) {
                                break;
                        }
                }
        }
        
        //[ek] TODO dublicated logic
}